package com.decheng.dao.impl;

import com.decheng.dao.BookDao;
import com.decheng.dao.base.BaseDao;
import com.decheng.pojo.Book;
import com.decheng.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {

    @Override
    public ArrayList<Book> getAllBooks() {
        Connection conn = null;
        ArrayList<Book> list = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "select * from books";
            list = this.getBeanList(conn, sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return list;
    }

    /**
     * 查询所有图书-分页查询
     *
     * @param currentPage
     * @param pageSize
     */
    @Override
    public ArrayList<Book> getAllBooksPagination(Integer currentPage, Integer pageSize) {
        Connection conn = null;
        ArrayList<Book> list = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "select * from books limit ? , ?";
            Integer offSet = (currentPage - 1) * pageSize;
            list = this.getBeanList(conn, sql, offSet, pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return list;
    }

    /**
     * 根据ID删除图书
     *
     * @param book
     */
    @Override
    public void removeBookById(Book book) {
        Connection conn = null;

        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "delete from books where bookId=?";
            this.update(conn, sql, book.getBookId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
    }

    /**
     * 添加图书
     *
     * @param book
     */
    @Override
    public void addBook(Book book) {
        Connection conn = null;

        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "insert into books values(?,?,?,?,?,?,?)";
            this.update(conn, sql,
                    book.getBookId(),
                    book.getBookName(),
                    book.getAuthor(),
                    book.getPrice(),
                    book.getSales(),
                    book.getStock(),
                    book.getImgPath());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
    }

    /**
     * 根据id查询图书
     *
     * @param book
     * @return
     */
    @Override
    public Book getBookById(Book book) {
        Connection conn = null;
        Book bean = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "select * from books where bookId=?";
            bean = this.getBean(conn, sql, book.getBookId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return bean;
    }

    /**
     * 根据id更新图书
     *
     * @param book
     */
    @Override
    public void updateBookById(Book book) {
        Connection conn = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "update books set bookName=?,author=?,price=?,sales=?,stock=? where bookId=?";
            this.update(conn, sql,
                    book.getBookName(),
                    book.getAuthor(),
                    book.getPrice(),
                    book.getSales(),
                    book.getStock(),
                    book.getBookId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
    }

    /**
     * 统计图书个数
     *
     * @return
     */
    @Override
    public Long count() {
        Connection conn = null;
        Long value = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "select count(*) from books";
            value = (Long) this.getValue(conn, sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return value;
    }
}
